package com.blb.io;

import java.io.*;

public class ObjectIODemo {

    public static void saveStudent(Student student,String path){
        //创建对象输出流
        try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path))){
            //保存对象
            out.writeObject(student);
            //清除缓冲区
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Student readStudent(String path){
        //创建对象输入流
        try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(path))){
            Student stu = (Student) in.readObject();
            return stu;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Student student = new Student("001","张三",20);
        saveStudent(student,"D:\\stu");
        Student stu = readStudent("D:\\stu");
        System.out.println(student == stu);
        System.out.println(student.getAddress() == stu.getAddress());
        System.out.println(stu);
        stu.sayHi();
    }
}
